package com.pipayshop.controller;

import com.pipayshop.domain.ShopCommodityHistory;
import com.pipayshop.entity.vo.PageDataVO;
import com.pipayshop.entity.vo.ResponseVO;
import com.pipayshop.exception.BusinessException;
import com.pipayshop.service.OnlineStoreShopCommodityHistoryService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author xiaoqiu
 * @date 2024/9/23 10:32
 * @description 网店用户商品历史记录表
 */
@RestController
@RequestMapping("/pipayshopapi/history")
@Slf4j
public class OnlineStoreShopCommodityHistoryController {

    @Autowired
    private OnlineStoreShopCommodityHistoryService shopCommodityHistoryService;

    @PostMapping("addHistory")
    @ApiOperation("新增用户浏览网店商品的历史记录")
    public ResponseVO addHistory(ShopCommodityHistory shopCommodityHistory) {
        try {
            boolean result = shopCommodityHistoryService.addHistory(shopCommodityHistory);
            if (!result) {
                throw new Exception();
            }
            return ResponseVO.getSuccessResponseVo("add_user_history_success");
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
            throw new BusinessException("add_user_history_failed");
        }
    }

    @PostMapping("deleteHistory/{userId}/{commodityId}")
    @ApiOperation("删除用户浏览网店商品的历史记录")
    public ResponseVO deleteHistory(@PathVariable String userId, @PathVariable String commodityId) {
        try {
            boolean result = shopCommodityHistoryService.deleteHistory(userId, commodityId);
            if (!result) {
                throw new Exception();
            }
            return ResponseVO.getSuccessResponseVo("delete_user_history_success");
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
            throw new BusinessException("delete_user_history_failed");
        }
    }

    /**
     * 定期轮询删除超过十天的浏览历史足迹记录
     */
    @PostMapping("orderDeleteHistory")
    @ApiOperation("定期轮询删除超过十天的浏览历史足迹记录")
    public ResponseVO<String> orderDeleteHistory() {
        try {
            boolean result = shopCommodityHistoryService.orderDeleteHistory();
            if (!result) {
                throw new Exception();
            }
            return ResponseVO.getSuccessResponseVo("定期轮询删除超过十天的浏览历史足迹记录");
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
            throw new BusinessException("delete_history_poll_failed");
        }
    }


    //    public ResponseVO getHistory(@PathVariable String userId){
//        try {
//            return ResponseVO.getSuccessResponseVo(shopCommodityHistoryService.getHistory(userId));
//        }catch (Exception e){
//            e.printStackTrace();
//            log.error(e.getMessage());
//            throw new BusinessException("get_user_history_failed");
//        }
//    }
    @GetMapping("getHistory/{page}/{limit}/{userId}")
    @ApiOperation("获取用户浏览网店商品的历史记录")
    public ResponseVO getHistory(@PathVariable Integer page, @PathVariable Integer limit, @PathVariable("userId") String userId) {
        try {
//            return ResponseVO.getSuccessResponseVo(shopCommodityHistoryService.getHistory(userId));
            PageDataVO pageDataVO = shopCommodityHistoryService.getHistory(page, limit, userId);
            return ResponseVO.getSuccessResponseVo(pageDataVO);
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
            throw new BusinessException("get_user_history_failed");
        }
    }

}
